package com.itcat.controller;/*
 * @Author: gonhsl
 * @Date:2022-04-04-13:22
 * */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itcat.common.CommonPage;
import com.itcat.common.CommonResult;
import com.itcat.mysql.pojo.SmsFlashPromotionProductRelation;
import com.itcat.mysql.vo.SmsFlashPromotionProductRelationVo;
import com.itcat.service.SmsFlashPromotionProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flashProductRelation")
public class SmsFlashPromotionProductRelationController {
    @Autowired
    private SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;
    /**
     * Description:分页查询不同场次关联及商品信息
     * date: 2022/4/4 13:22
     * @author: chp
     * @since JDK 1.8
     */
@GetMapping("/list")
    public CommonResult list(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             Integer flashPromotionId,
                             Integer flashPromotionSessionId){
    List<SmsFlashPromotionProductRelationVo> vos = smsFlashPromotionProductRelationService.list(pageNum, pageSize, flashPromotionId, flashPromotionSessionId);
    Page<SmsFlashPromotionProductRelationVo> page = new Page<>(pageNum, pageSize);
    page.setRecords(vos);
    page.setTotal(vos.size());
    if (vos.size()%pageSize==0){
        page.setPages(vos.size()/pageSize);
    }else {
        page.setPages(vos.size()/pageSize+1);

    }
    CommonPage<SmsFlashPromotionProductRelationVo> restPage = CommonPage.restPage(page);
    return CommonResult.success(restPage);
}

    /**
     * Description:批量选择商品添加关联
     * date: 2022/4/4 13:22
     *
     * @author: chp
     * @since JDK 1.8
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody List<SmsFlashPromotionProductRelation> relationList) {
smsFlashPromotionProductRelationService.create(relationList);
        return CommonResult.success();
    }

    /**
     * Description:删除关联
     * date: 2022/4/4 13:22
     *
     * @author: chp
     * @since JDK 1.8
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id) {
        smsFlashPromotionProductRelationService.delete(id);
        return CommonResult.success();
    }

    /**
     * Description:删除关联
     * date: 2022/4/4 13:22
     *
     * @author: chp
     * @since JDK 1.8
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Integer id,@RequestBody SmsFlashPromotionProductRelation relation) {
        smsFlashPromotionProductRelationService.update(id,relation);
        return CommonResult.success();
    }

    /**
     * Description:获取管理商品促销信息
     * date: 2022/4/4 13:22
     *
     * @author: chp
     * @since JDK 1.8
     */
    @PostMapping("/{id}")
    public CommonResult findOne(@PathVariable Integer id) {
        SmsFlashPromotionProductRelation one = smsFlashPromotionProductRelationService.findOne(id);
        return CommonResult.success(one);
    }
}
